package com.hunttown.mes.manage.controller._erp;

import com.hunttown.mes.manage.controller.common.ModelService;
import com.hunttown.mes.manage.service.TransportPurchaseDetailManageService;
import com.hunttown.mes.manage.service.TransportPurchaseManageService;
import com.hunttown.mes.rpc.domain.TransportPurchaseDetailDTO;
import com.hunttown.mes.rpc.domain.query.TransportPurchaseDetailDTOQuery;
import com.hunttown.common.domain.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import static com.hunttown.mes.common.utils.PageUtils.PagePackage;

/**
 * @title: 采销提报
 * @author: wangjunfu
 * @date: 2020-04-25
 * @description: 不再使用，新版改为采购单
 */
@Controller
@RequestMapping(value = "/erp/purchase-apply")
public class PurchaseDetailApplyController {
    private final static Logger logger = LoggerFactory.getLogger(PurchaseDetailApplyController.class);

    @Autowired
    private TransportPurchaseDetailManageService purchaseDetailService;
    @Autowired
    private TransportPurchaseManageService purchaseService;
    @Autowired
    private ModelService modelService;

    /**
     * 提报列表
     *
     * @param model
     * @param pn
     * @param purchasetype  采购类型：1汽车配件 2日常采购
     * @param rpurchasetype 入库形式：1采购 2提报
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model, Integer pn, Integer purchasetype, Integer rpurchasetype, Integer rPurchaser) {
        if (purchasetype == null || purchasetype <= 0 || rpurchasetype == null || rpurchasetype <= 0) {
            throw new RuntimeException("查询提报信息出错！");
        }

        if (rPurchaser == null) {
            rPurchaser = -9999;
        }

        if (pn == null || pn <= 0) {
            pn = 1;
        }

        TransportPurchaseDetailDTOQuery query = new TransportPurchaseDetailDTOQuery();
        query.setrPurchaser(rPurchaser);
        query.setPn(pn);
        query.setPurchaseType(purchasetype);
        query.setrPurchaseType(rpurchasetype);
        query.setOrderBy("order by id desc");
        Page<TransportPurchaseDetailDTO> dataPage = purchaseDetailService.getForPage(query);

        // 实时更新列表信息
        if (dataPage != null && dataPage.getItems().size() > 0) {
            for (TransportPurchaseDetailDTO item : dataPage.getItems()) {
                item.setPartName(modelService.getPurchaseName(item.getPartId()));
                item.setrPurchaserName(modelService.getManageName(item.getrPurchaser()));
            }
        }

        model.addAttribute("page", dataPage);
        model.addAttribute("PagePackage", PagePackage(dataPage.getPn(), dataPage.getStart(), dataPage.getPageSize(), dataPage.getTotalPages(), dataPage.getTotalItems()));

        model.addAttribute("purchasetype", purchasetype);
        model.addAttribute("rpurchasetype", rpurchasetype);
        model.addAttribute("rPurchaser", rPurchaser);

        if (purchasetype == 1) {
            model.addAttribute("title", "配件提报");
        } else {
            model.addAttribute("title", "采购提报");
        }

        //获取采购人
        modelService.getManagerPage(model);

        return "erp/purchase-apply/index";
    }

}
